<?php
$menumark = 'redpack';
switch ($act) {
    //#####################@ 修改分类 @#####################//
    case 'edit':
        if (isGet()) {

            $redpack = $db->pe_select('redpack',array('redpack_id'=>$_g_id));//
            $redpack['redpack_enddate'] = date('Y-m-d',$redpack['redpack_enddate']);
            $house = $db->pe_selectall('house');//房型
            $redpack_person = $db->pe_select('user',array('user_wxid'=>$redpack['redpack_wxid']));
            include(pe_tpl('redpack_edit.html'));
        } else if (isPost()) {

            $_p_info['redpack_userid'] = implode('|',$_p_redpack_wxid);//如果是给多人分配这个红包就要
            $_p_info['redpack_enddate'] = strtotime($_p_info['redpack_enddate']);

            if($db->pe_update('redpack',array('redpack_id'=>$_g_id),$_p_info)){
                pe_success('修改红包成功');
            }else{
                pe_error('修改红包失败');
            }

        }



        break;

    case 'del':

        $result = $db->pe_delete('redpack', array('redpack_id' => $_g_id));
        //同时删除wxgg_fj里的相同房型，wxgg_houseimg里的相同房型
        //怎么删
        if ($result) {
            pe_success('删除成功!');
        } else {
            pe_error('删除失败...');
        }

        break;

    case 'add':
        if (isGet()) {
            $house = $db->pe_selectall('house');
            $user = $db->pe_selectall('user',"","user_wxid");
            $all_user = "";
            foreach ($user as $k => $v){
                if($k==0){
                    $all_user = $v['user_wxid'];
                }else{
                    $all_user = $all_user ."|" . $v['user_wxid'];
                }

            }

            include(pe_tpl('redpack_add.html'));
        } else if (isPost()) {


            $_p_info['redpack_enddate'] = strtotime($_p_info['redpack_enddate']);
            $user = $db->pe_selectall('user');

            if($_p_info['redpack_isall']==1){
                for($i=0;$i<count($user);$i++){
                    $_p_info['redpack_userid'] = $user[$i]['user_id'];
                    $db->pe_insert('redpack',$_p_info);

                }
            }else{
                $_p_info['redpack_userid'] = $_p_redpack_wxid;
//                mp($_p_info);
                $db->pe_insert('redpack',$_p_info);

            }


           pe_success('添加红包成功');


            include(pe_tpl('redpack_add.html'));
        }


        break;

    case 'find_by_name':

        $sql = "select * from wxgg_user where user_wxname like '%{$_g_name}%'";
        $user = $db->sql_selectall($sql);
        ob_clean();
        echo json_encode($user);

        break;


    //#####################@ 分类列表 @#####################//
    default :

        $redpack = $db->pe_selectall('redpack');
        foreach ($redpack as $k => $v){
            $redpack[$k]['redpack_enddate'] = date('Y-m-d',$v['redpack_enddate']);
            $house = $db->pe_select('house',array('house_id'=>$v['redpack_houseid']));
            $redpack[$k]['redpack_houseid'] = $house['house_name'];
            $user = $db->pe_select('user',array('user_id'=>$v['redpack_userid']));
            $redpack[$k]['redpack_wxid'] = $user['user_wxname'];

            if($v['redpack_enddate']<time()){
                $redpack[$k]['redpack_state'] = "已过期";

            }else{
                switch ($v['redpack_state']){


                    case'0':

                        $redpack[$k]['redpack_state'] = "未使用";
                        break;

                    case'1':

                        $redpack[$k]['redpack_state'] = "已使用";
                        break;


                    default:

                        break;

                }
            }

        }
        include(pe_tpl('redpack_list.html'));

        break;
}
?>